package cn.kylinos.springportal.access.service.impl;

import cn.kylinos.springportal.access.dao.AccesslogRepository;
import cn.kylinos.springportal.access.model.Accesslog;
import cn.kylinos.springportal.access.service.AccesslogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.Instant;
import java.util.List;

/**
 * @author lichenghong
 */
@Service
public class AccesslogServiceImpl implements AccesslogService {
    @Autowired
    private AccesslogRepository accesslogRepository;

    @Override
    public List<Accesslog> findAll() {

        return accesslogRepository.findAll();
    }

    @Override
    public Page<Accesslog> findAll(Pageable pageable) {
        return accesslogRepository.findAll(pageable);
    }

    @Override
    public Accesslog findById(Integer logId) {
        return accesslogRepository.findById(logId).orElseThrow();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Accesslog create(Accesslog accesslog) {
        // 不需要用户传入的参数需要手动设置
        accesslog.setId(null);
        accesslog.setCreatedAt(Instant.now());
        accesslog.setUpdatedAt(Instant.now());
        return accesslogRepository.save(accesslog);
    }

    // 日志没有修改和删除操作
}
